/**
 * Created by steven on 2016/3/4.
 *
 * This jquery.fastinput.js is based by jquery.min-1.11.3.js
 * when browser's event is "Enter", the input elements in the form auto switch to next input elements,
 * if has callback function ,the function will be execute in the last input element.
 *
 */
+(
    $.extend(
        {
            fastinput: {
                load: function (settings) {
                    if (settings) {
                        $.extend($.fastinput.setting, settings);
                    }

                    var inputs = $('form').find(":input");
                    inputs.each(function (i, ele) {
                        if ($(this).hasClass('Wdate')) {
                            $(this).bind("focus", function () {
                                this.onclick();
                                $dp.hide();
                            });
                        }

                        $(ele).keydown(function (event) {
                            if (13 == event.keyCode) {
                                if ($.fastinput.setting.refresh) {
                                    inputs = $('form').find(":input");
                                    i = inputs.index(ele);
                                }

                                if ($.fastinput.isLastEditableElement(inputs, i) && typeof $.fastinput.setting.callback === "function") {
                                    $.fastinput.setting.callback();
                                    return;
                                }

                                $($.fastinput.getNextEditableElement(inputs, i)).focus().select();
                            }
                        });
                    });
                },
                isLastEditableElement: function (elements, index) {
                    if (index === elements.size() - 1) {
                        return true;
                    }
                    return $(elements[index + 1]).is($.fastinput.setting.editable) ? false : this.isLastEditableElement(elements, index + 1);
                },
                getNextEditableElement: function (elements, index) {
                    index = index < elements.size() ? index : -1;
                    return $(elements[index + 1]).is($.fastinput.setting.editable) ? elements[index + 1] : this.getNextEditableElement(elements, index + 1);
                },
                setting: {
                    editable: ":input:not([type='hidden'],:disabled,:hidden,:radio)",
                    refresh: false,
                    callback: null
                }
            }
        })
)();